package com.yukefms.rule.state;

import com.yukefms.engine.running.EventCode;
import com.yukefms.engine.running.RunningContext;
import com.yukefms.engine.running.RunningEvent;
import com.yukefms.jointpoint.BodyPointCloud;
import com.yukefms.rule.interfaces.BARule;
import com.yukefms.rule.interfaces.BAStageRule;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Description
 *    *CASE 4. <unmatched state> The logic for processing read pointCloud at UNMATCHED state was defined as follow:
 *          *
 *          *     update TrialCounter
 *          *     if trial is out:
 *          *         transition to FAILED state
 *          *     else
 *          *         reset Timer and input_error Counter
 *          *         transition to IN_ACTIVATED state
 * @Author Ping
 * @Date 2021/6/29 10:25
 **/
public class InActivatedState extends BARuleState {
    public static Logger logger = LogManager.getLogger(InActivatedState.class);

    public InActivatedState(final BAStageRule relatedRule) {
        super(relatedRule);
    }

    /***
     * *    check the rule is triggered.
     *          *    if it is :
     *          *        transition to ACTIVATED state
     *          *     else
     *          *        return for the next pointCloud
     * @param pointCloud
     * @param context
     */
    @Override
    protected void processRelevantPointsCloud(BodyPointCloud pointCloud, RunningContext context) {
        context.notifyRunningEventListener(EventCode.INACTIVED,
                                             this.relatedRule.getTriggerName(),
                                              this.relatedRule.getTrigger().getDescription());
        if (! this.relatedRule.isActivated(pointCloud, context)) {
            // if no activated, then skip for the next point cloud.
          //  logger.error("the Rule-{} is NOT ACTIVATED for current point cloud.", this.relatedRule.ruleName()) ;
            return ;
        }
        /**
         * transition to ACTIVATED STATE
         */
        logger.error("the Rule-{} is ACTIVATED for current point cloud.", this.relatedRule.ruleName()) ;
        context.notifyRunningEventListener(EventCode.ACTIVED, this.relatedRule.currentStage().getName());
        this.relatedRule.setRuleState(RuleStateFlyWeight.getState(RuleStateFlyWeight.ACTIVATED));
        // reset caching content
        context.cacheTriggerStartPointCloud(pointCloud);
        context.updateScore(this.relatedRule.getTrigger().getScore());
    }

}
